//
//  RootViewController.m
//  WhatsChat
//
//  Created by lanou3g on 16/3/24.
//  Copyright © 2016年 tanchen. All rights reserved.
//

#import "RootViewController.h"

// 右上角小列表依赖头文件
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
// 二维码 扫一扫依赖的头文件
#import "LBXScanView.h"
#import <objc/message.h>
#import "LBXScanResult.h"
#import "LBXScanWrapper.h"
#import "SubLBXScanViewController.h"
#import "MyQRViewController.h"
#import "AddFrinedViewController.h"
@interface RootViewController ()

@end

@implementation RootViewController


#pragma mark-----搜索-----
- (void)searchAction{
    NSLog(@"search");
}
#pragma mark-----添加-----
- (void)addAction{
    NSLog(@"add");
    NSMutableArray *obj = [NSMutableArray array];
    
    for (NSInteger i = 0; i < [self titles].count; i++) {
        
        WBPopMenuModel * info = [WBPopMenuModel new];
        info.image = [self images][i];
        info.title = [self titles][i];
        [obj addObject:info];
    }
    
    // Do any additional setup after loading the view.
    
    [[WBPopMenuSingleton shareManager]showPopMenuSelecteWithFrame:130
                                                             item:obj
                                                           action:^(NSInteger index) {
                                                               NSLog(@"index:%ld",(long)index);
                                                               if (index == 0) {
                                                                   NSLog(@"二维码");
                                                                   [self myQrviewAction];
                                                               }else if (index == 1){
                                                                   NSLog(@"扫一扫");
                                                                   if (![self cameraPemission])
                                                                   {
                                                                       [self showError:@"没有摄像机权限"];
                                                                       return;
                                                                   }
                                                                   
                                                                   SEL normalSelector = NSSelectorFromString(@"changeColor");
                                                                   if ([self respondsToSelector:normalSelector]) {
                                                                       
                                                                       ((void (*)(id, SEL))objc_msgSend)(self, normalSelector);
                                                                   }
                                                               }else if (index == 2){
                                                                   NSLog(@"加好友");
                                                                   [self addFriendAction];
                                                                   
                                                               }else if (index == 3){
                                                                   NSLog(@"创建组");
                                                                   
                                                               }
                                                           }];
}

#pragma mark-----我的二维码-----
- (void)myQrviewAction{
    MyQRViewController *vc = [MyQRViewController new];
//    vc.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStyleDone) target:nil action:nil];
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark-----跳转到加好友界面-----
- (void)addFriendAction{

    AddFrinedViewController *addVC = [AddFrinedViewController new];
    [self.navigationController pushViewController:addVC animated:YES];

}

#pragma mark -----自定义扫一扫4个角及矩形框颜色-----
- (void)changeColor
{
    
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    
    //扫码框周围4个角的类型设置为在框的上面
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_On;
    //扫码框周围4个角绘制线宽度
    style.photoframeLineW = 6;
    
    //扫码框周围4个角的宽度
    style.photoframeAngleW = 24;
    
    //扫码框周围4个角的高度
    style.photoframeAngleH = 24;
    
    //显示矩形框
    style.isNeedShowRetangle = YES;
    
    //动画类型：网格形式，模仿支付宝
    style.anmiationStyle = LBXScanViewAnimationStyle_NetGrid;
    
    
    style.animationImage = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_part_net"];
    
    //码框周围4个角的颜色
    style.colorAngle = [UIColor colorWithRed:0.306 green:0.718 blue:0.443 alpha:1.000];
    
    //矩形框颜色
    style.colorRetangleLine = [UIColor colorWithRed:247/255. green:202./255. blue:15./255. alpha:1.0];
    //    [UIColor colorWithRed:0.675 green:0.969 blue:0.923 alpha:1.000]
    //非矩形框区域颜色
    style.red_notRecoginitonArea = 0.675;
    style.green_notRecoginitonArea = 0.969;
    style.blue_notRecoginitonArea = 0.923;
    style.alpa_notRecoginitonArea = 0.2;
    
    SubLBXScanViewController *vc = [SubLBXScanViewController new];
    vc.style = style;
    
    //开启只识别矩形框内图像功能
    vc.isOpenInterestRect = YES;
    vc.isQQSimulator = YES;
    vc.isVideoZoom = YES;
    // 由于返回之后没有TabBar,所以添加下面这行代码
    vc.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:nil action:nil];
    [self.navigationController pushViewController:vc animated:YES];
    //    [self.selectedViewController presentViewController:vc animated:YES completion:nil];
    
}
// 显示错误信息
- (void)showError:(NSString*)str
{
    [LBXAlertAction showAlertWithTitle:@"提示" msg:str chooseBlock:nil buttonsStatement:@"知道了",nil];
}
// 判断权限
- (BOOL)cameraPemission
{
    BOOL isHavePemission = NO;
    if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
    {
        AVAuthorizationStatus permission =
        [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        
        switch (permission) {
            case AVAuthorizationStatusAuthorized:
                isHavePemission = YES;
                break;
            case AVAuthorizationStatusDenied:
            case AVAuthorizationStatusRestricted:
                break;
            case AVAuthorizationStatusNotDetermined:
                isHavePemission = YES;
                break;
        }
    }
    
    return isHavePemission;
}

#pragma mark-----添加小列表-----
- (NSArray *) titles {
    return @[@"二维码",
             @"扫一扫",
             @"加好友",
             @"创建组"];
}
#pragma mark-----添加小列表图标-----
- (NSArray *) images {
    return @[@"iconfont-erweima",
             @"iconfont-saoyisao",
             @"iconfont-tianjiahaoyou",
             @"iconfont-taolunhui"];
}

- (void)getNavigetiontem:(UIViewController *)newViewController flag:(NSInteger)flag{
    // 设置左边Item
    newViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"ChatOne" style:(UIBarButtonItemStyleDone) target:nil action:nil];
    // 设置右边Item
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(searchAction)];
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addAction)];
    NSArray *itemArray = [[NSArray alloc] initWithObjects:addItem,searchItem, nil];
#pragma mark-----如果某个界面需要添加多个navigationItem可以在下面添加-----
    if (flag == 0) {
        // 消息界面
        newViewController.navigationItem.rightBarButtonItems = itemArray;
    }else if (flag == 1){
        // 通讯录界面
        newViewController.navigationItem.rightBarButtonItems = itemArray;
    }else if (flag == 2){
        // 发现界面
        newViewController.navigationItem.rightBarButtonItems = itemArray;
    }else {
        // 我的界面
        newViewController.navigationItem.rightBarButtonItems = itemArray;
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // 导航栏赋值
    [self getNavigetiontem:self flag:0];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
